﻿using B.S.DataBase.MyProject.Domain.DiagnosisAndTreatmentManagement;
using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.HealthMonitoringCommand;
using SqlSugar;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.HealthMonitoringCommandHandler
{
    /// <summary>
    /// 血压
    /// </summary>
    public class GetBloodPressureCommandHandler : IRequestHandler<GetBloodPressureCommand, APIResult<List<BloodPressure>>>
    {
        private readonly SqlSugarContext sqlSugar;

        public GetBloodPressureCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        public async Task<APIResult<List<BloodPressure>>> Handle(GetBloodPressureCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<BloodPressure>> result = new APIResult<List<BloodPressure>>();
                var entity = sqlSugar.db.Queryable<BloodPressure>().Where(x => x.UserId == request.UserId);
                if (request.StartTime != null && request.EndTime != null)
                {
                    entity = entity.Where(x => x.CreateTime >= request.StartTime && x.CreateTime <= request.EndTime);
                }
                entity = entity.OrderByDescending(x => x.PId);
                if (entity != null)
                {
                    result.Code = APIEnum.查询成功;
                    result.Msg = APIEnum.查询成功.ToString();
                    result.data = entity.ToList();
                }
                else
                {
                    result.Code = APIEnum.查询失败;
                    result.Msg = APIEnum.查询失败.ToString();
                }
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
